﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Engine.Graphics
{
    public class ModelEntity : DrawableEntity
    {
        protected List<MeshEntity> meshEntities;
        //MeshEntity[] entities;
        Matrix[] absoluteBoneTransforms;



        public ModelEntity(Entity parent, Model model)
            : base(parent)
        {
            absoluteBoneTransforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(absoluteBoneTransforms);

            meshEntities = new List<MeshEntity>(model.Meshes.Count);
            for (int i = 0; i < model.Meshes.Count; i++)
            {
                meshEntities.Add(new MeshEntity(this, model.Meshes[i], absoluteBoneTransforms[model.Meshes[i].ParentBone.Index]));
                //meshEntities.Add(new MeshEntity(model.Meshes[i], Matrix.Identity));
            }
            OffsetWorld = absoluteBoneTransforms[0];
            //CABLE:
            this.BoundingBox = meshEntities[0].BoundingBox;
            this.BoundingSphere = meshEntities[0].BoundingSphere;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < meshEntities.Count; i++)
            {
                MeshEntity entity = meshEntities[i];
                if (entity.Visible)
                {
                    //entity.ParentWorld = parentWorld * World;
                    entity.Draw(gameTime);
                }
            }
            //base.Draw(gameTime);
        }
    }
}
